﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Mindfor.Web.Data;
using Mindfor.Web.Models;
using NHibernate;
using NHibernate.Linq;

namespace Mindfor.Web.Modules.Pages
{
	/// <summary>
	/// Enables pages in Site.
	/// </summary>
	public class PagesModule : ModuleBase
	{
		public override string FriendlyName
		{
			get { return "Страницы"; }
		}

		/// <summary>
		/// Returns modules names current one dependent on.
		/// </summary>
		public override string[] GetDependencyModules()
		{
			return new string[] { CmsConstants.SystemModuleName };
		}

		/// <summary>
		/// Registers module. For example, custom routes.
		/// </summary>
		/// <param name="info">Registration data.</param>
		public override void Register(ModuleRegistrationData info)
		{
			info.GlobalFilters.Add(new PageLoadFilter());
			base.Register(info);
		}

		/// <summary>
		/// Performs search in module.
		/// </summary>
		/// <param name="data">Current data session.</param>
		/// <param name="url">Url helper.</param>
		/// <param name="text">Text to search.</param>
		/// <returns>Search results.</returns>
		public override IEnumerable<SearchResult> Search(ISession data, UrlHelper url, string text)
		{
			return data.Query<PageText>()
				.Where(r => r.Title.Contains(text) || r.Text.Contains(text))
				.ToList()
				.Select(r => new SearchResult(this, r.Title)
				{
					Url = url.Page(r.Page.Id)
				})
				.ToList();
		}
	}
}
